package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.Socket;
import java.util.*;

/*
    客服类： 专门用来和用户打交道的
        1. 接收用户键盘录入的数据      -- 用户请求
        2. 找业务员完成具体的业务功能操作
        3. 根据业务员的反馈结果,来给出用户相关的一个响应信息
 */
public abstract class BaseStudentController {
    private static final Logger LOGGER = LoggerFactory.getLogger(BaseStudentController.class);
    public Scanner sc = new Scanner(System.in);


    // 开启学生管理系统, 并展示学生管理系统菜单
    public final void start() throws IOException {
        lo:while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出<学生>管理系统");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    deleteStudent();
                    break;
                case "3":
                    updateStudent();
                    break;
                case "4":
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("感谢您使用学生管理系统, 再见!");
                    return;
                default:
                    System.out.println("您的输入有误, 请重新输入");
                    break;
            }
        }
    }

    //添加学生功能
    public final void addStudent() {
        try {
            //1. 接收用户输入的信息
            String sid = inputStudentId("false");
            Student stu = getStudent(sid);

            //将当前学生信息通过socket发送给服务器,在服务端进行持久化存储
            LOGGER.info("将当前要添加的学生信息发送给服务器");
            Socket socket = getSocket();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("[1]," + stu.toString());
            bw.flush();
            socket.shutdownOutput();

            //接收服务器反馈的结果
            LOGGER.info("接收服务器反馈的添加结果");
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String res = br.readLine();
            //3. 根据业务员反馈的结果,输出提示信息到控制台
            if (res.equals("true")) {
                System.out.println("添加成功");
            } else {
                System.out.println("添加失败");
            }
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
        }

    }

    //删除学生
    public final void deleteStudent() {
        try {
            String sid = inputStudentId("true");
            LOGGER.info("将当前要删除的学生信息发送给服务器");
            Socket socket = getSocket();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("[2]," + sid.toString());
            bw.flush();
            socket.shutdownOutput();

            //接收服务器反馈的结果
            LOGGER.info("接收服务器反馈的删除结果");
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String res = br.readLine();
            //3. 根据业务员反馈的结果,输出提示信息到控制台
            if (res.equals("true")) {
                System.out.println("删除成功");
            } else {
                System.out.println("删除失败");
            }
        } catch (IOException e) {
            LOGGER.error(e.getMessage());
        }


    }

    //修改学生
    public final void updateStudent() {
        try {
            String sid = inputStudentId("true");
            LOGGER.info("将当前要修改的学生信息发送给服务器");
            Socket socket = getSocket();
            Student stu = getStudent(sid);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("[3]," + stu.toString());
            bw.flush();
            socket.shutdownOutput();

            //接收服务器反馈的结果
            LOGGER.info("接收服务器反馈的修改结果");
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String res = br.readLine();
            //3. 根据业务员反馈的结果,输出提示信息到控制台
            if (res.equals("true")) {
                System.out.println("修改成功");
            } else {
                System.out.println("修改失败");
            }
        } catch (IOException e) {
            LOGGER.error(e.getMessage());
        }
    }

    //查询所有学生功能
    public final void findAllStudent() {
        try {
            LOGGER.info("将查询所有学生信息的指令发送给服务器");
            Socket socket = new Socket("127.0.0.1", 10000);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("[4]");
            bw.flush();
            socket.shutdownOutput();
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            ArrayList<Student> list = (ArrayList<Student>) ois.readObject();
            System.out.println("学号\t\t姓名\t\t\t 年龄\t\t生日");
            //遍历数组,展示学生信息
            for (int i = 0; i < list.size(); i++) {
                Student stu = list.get(i);  // Student stu = null;
                //判断当前索引位置的元素是否为null,不为null的才展示
                if (stu != null) {
                    System.out.println(stu.getSid() + "\t\t" + stu.getName() + "\t\t\t " + stu.getAge() + "\t\t\t" + stu.getBirthday());
                }
            }
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
        }


    }

    //获取用户录入的id
    public final String inputStudentId(String exists) throws IOException {
        System.out.println("请输入id:");
        String id;

            /*2. 调用业务员的判断学号是否存在方法isExists,校验用户录入的id在数组容器中是否存在
                不在:   让用户重新录入  -- while(true)
                在 :   往下执行删除操作*/

        while (true) {
            id = sc.next();

            Socket socket = getSocket();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("[5]," + id);
            bw.flush();
            socket.shutdownOutput();

            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String s = br.readLine();
            if (exists.equals(s)) {
                break;
            } else {
                System.out.println("当前学号无效,请重新输入:");
            }
        }
        return id;
    }

    //键盘录入学生信息,封装学生对象并返回
    public abstract Student getStudent(String sid);

    private Socket getSocket() throws IOException {
        return new Socket("127.0.0.1", 10000);
    }

}
